<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目" style="width: 100%;" filterable clearable
          @change="handleQuery">
          <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName"
            :value="item.projectId" />
        </el-select>
      </el-form-item>
      <el-form-item label="标段名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入标段名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="施工单位" prop="sgdwName">
        <el-select v-model="queryParams.sgdwId" placeholder="请选择单位" style="width: 100%;" filterable clearable
          @change="handleQuery">
          <el-option v-for="item in deptOptions2" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="监理单位" prop="jldwName">
        <el-select v-model="queryParams.jldwId" placeholder="请选择单位" style="width: 100%;" filterable clearable
          @change="handleQuery">
          <el-option v-for="item in deptOptions3" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select> </el-form-item>
      <el-form-item label="审计单位" prop="sjdwName">
        <el-select v-model="queryParams.sjdwId" placeholder="请选择单位" style="width: 100%;" filterable clearable
          @change="handleQuery">
          <el-option v-for="item in deptOptions4" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select> </el-form-item>
      <el-form-item label="设计单位" prop="shejdwName">
        <el-select v-model="queryParams.shejdwId" placeholder="请选择单位" style="width: 100%;" filterable clearable
          @change="handleQuery">
          <el-option v-for="item in deptOptions5" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['base:bsBd:add']">新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['base:bsBd:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['base:bsBd:remove']">删除</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['base:bsBd:export']">导出</el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>
    </el-form>
    <el-table border stripe v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属项目" align="center" prop="projectName" min-width="120" show-overflow-tooltip />
      <el-table-column label="标段名称" align="center" prop="name" min-width="120" show-overflow-tooltip />
      <el-table-column label="施工单位" align="center" prop="sgdwName" min-width="120" show-overflow-tooltip />
      <el-table-column label="监理单位" align="center" prop="jldwName" min-width="120" show-overflow-tooltip />
      <el-table-column label="审计单位" align="center" prop="sjdwName" min-width="120" show-overflow-tooltip />
      <el-table-column label="设计单位" align="center" prop="shejdwName" min-width="120" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['base:bsBd:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['base:bsBd:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改标段对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="标段名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入标段名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属项目" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择项目" style="width: 100%;" filterable
                @change="setProjectName()">
                <el-option v-for="item in projectOptions" :key="item.projectId" :label="item.projectName"
                  :value="item.projectId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="施工单位" prop="sgdwId">
              <el-select v-model="form.sgdwId" placeholder="请选择单位" style="width: 100%;" filterable
                @change="setDeptName(2)">
                <el-option v-for="item in deptOptions2" :key="item.deptId" :label="item.deptName"
                  :value="item.deptId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监理单位" prop="jldwId">
              <el-select v-model="form.jldwId" placeholder="请选择单位" style="width: 100%;" filterable
                @change="setDeptName(3)">
                <el-option v-for="item in deptOptions3" :key="item.deptId" :label="item.deptName"
                  :value="item.deptId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="审计单位" prop="sjdwId">
              <el-select v-model="form.sjdwId" placeholder="请选择单位" style="width: 100%;" filterable
                @change="setDeptName(4)">
                <el-option v-for="item in deptOptions4" :key="item.deptId" :label="item.deptName"
                  :value="item.deptId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设计单位" prop="shejdwId">
              <el-select v-model="form.shejdwId" placeholder="请选择单位" style="width: 100%;" filterable
                @change="setDeptName(5)">
                <el-option v-for="item in deptOptions5" :key="item.deptId" :label="item.deptName"
                  :value="item.deptId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>





      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsBd, getBsBd, delBsBd, addBsBd, updateBsBd } from "@/api/base/bsBd";
import { listDeptByType } from "@/api/system/dept";
import { queryProjectSelect } from "@/api/base/bsProject";
import { mapGetters } from "vuex";
export default {
  name: "BsBd",
  data () {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 标段表格数据
      dataList: [],
      projectOptions: [],
      deptOptions2: [],
      deptOptions3: [],
      deptOptions4: [],
      deptOptions5: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: undefined,
        parentId: undefined,
        name: undefined,
        sort: undefined,
        sgdwId: undefined,
        sgdwName: undefined,
        jldwId: undefined,
        jldwName: undefined,
        sjdwId: undefined,
        sjdwName: undefined,
        shejdwId: undefined,
        shejdwName: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "所属项目不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "标段名称不能为空", trigger: "blur" }
        ],
        sgdwId: [
          { required: true, message: "施工单位不能为空", trigger: "change" }
        ],
        jldwId: [
          { required: true, message: "监理单位不能为空", trigger: "change" }
        ],
        sjdwId: [
          { required: true, message: "审计单位不能为空", trigger: "change" }
        ],
        // shejdwId: [
        //   { required: true, message: "设计单位不能为空", trigger: "change" }
        // ],
      }
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["sysProjectId", "sysProjectList"])
  },
  watch: {
    sysProjectId (newVal, oldVal) {
      // 系统顶部选中项目变化
      this.queryParams.projectId = newVal;
      this.handleQuery();
    }
  },
  created () {
    this.queryParams.projectId = this.sysProjectId;
    this.projectOptions = this.sysProjectList;
    this.getList();
    // this.getProjectSelect();
    this.getDeptList(2);
    this.getDeptList(3);
    this.getDeptList(4);
    this.getDeptList(5);
  },
  methods: {
    /** 查询标段列表 */
    getList () {
      this.loading = true;
      listBsBd(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取项目下拉
    getProjectSelect () {
      queryProjectSelect().then(response => {
        this.projectOptions = response.data;
      });
    },
    // 获取建设单位
    getDeptList (deptType) {
      const params = {
        deptType: deptType
      };
      listDeptByType(params).then(response => {
        const options = response.data;
        switch (deptType) {
          case 2:
            this.deptOptions2 = options;
            break;
          case 3:
            this.deptOptions3 = options;
            break;
          case 4:
            this.deptOptions4 = options;
            break;
          case 5:
            this.deptOptions5 = options;
            break;
        }
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        projectId: this.sysProjectId,
        parentId: undefined,
        name: undefined,
        remark: undefined,
        sort: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        sgdwId: undefined,
        sgdwName: undefined,
        jldwId: undefined,
        jldwName: undefined,
        sjdwId: undefined,
        sjdwName: undefined,
        shejdwId: undefined,
        shejdwName: undefined
      };
      this.resetForm("form");
      this.setProjectName();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    setDeptName (type) {
      let info = {};
      switch (type) {
        case 2:
          info = this.deptOptions2.find(item => item.deptId === this.form.sgdwId);
          this.form.sgdwName = info.deptName;
          break;
        case 3:
          info = this.deptOptions3.find(item => item.deptId === this.form.jldwId);
          this.form.jldwName = info.deptName;
          break;
        case 4:
          info = this.deptOptions4.find(item => item.deptId === this.form.sjdwId);
          this.form.sjdwName = info.deptName;
          break;
        case 5:
          info = this.deptOptions5.find(item => item.deptId === this.form.shejdwId);
          this.form.shejdwName = info.deptName;
          break;
      }
    },
    setProjectName () {
      const info = this.projectOptions.find(item => item.projectId === this.form.projectId);
      this.form.projectName = info.projectName;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加标段";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getBsBd(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改标段";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBsBd(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBsBd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除标段名称为"' + row.name + '"的数据项？').then(() => {
        this.loading = true;
        return delBsBd(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('base/bsBd/export', {
        ...this.queryParams
      }, `bsBd_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
